package com.example.otherincome;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.text.BreakIterator;

public class OtherIncomeActivity extends AppCompatActivity {

    private EditText etIncome, etSource;
    private Button btnSave;

    private DatabaseHelper dbHelper;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_income);

        etIncome = findViewById(R.id.etIncome);
        etSource = findViewById(R.id.etSource);
        btnSave = findViewById(R.id.btnSave);

        dbHelper = new DatabaseHelper(this);

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveIncome();
            }
        });
    }

    private void saveIncome() {
        String income = etIncome.getText().toString().trim();
        BreakIterator etType = null;
        String category = "其他收入";

        if (income.isEmpty() || category.isEmpty()) {
            Toast.makeText(this, "请输入收入金额和类型", Toast.LENGTH_SHORT).show();
            return;
        }

        SQLiteDatabase db = dbHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(DatabaseContract.IncomeEntry.COLUMN_INCOME, income);
        values.put(DatabaseContract.IncomeEntry.COLUMN_CATEGORY, category);


        long newRowId = db.insert(DatabaseContract.IncomeEntry.TABLE_NAME, null, values);

        if (newRowId != -1) {
            Toast.makeText(this, "收入记录保存成功", Toast.LENGTH_SHORT).show();
            etIncome.setText("");
            etType.setText("");

            updateTotalIncome(db);
        } else {
            Toast.makeText(this, "保存失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }

    private void updateTotalIncome(SQLiteDatabase db) {
        String query = "SELECT SUM(" + DatabaseContract.IncomeEntry.IncomeRecordEntry.COLUMN_INCOME + ") FROM " +
                DatabaseContract.IncomeEntry.IncomeRecordEntry.TABLE_NAME;

        Cursor cursor = db.rawQuery(query, null);
        if (cursor.moveToFirst()) {
            double totalIncome = cursor.getDouble(0);
            // 保存总收入到总收入表
            ContentValues totalIncomeValues = new ContentValues();
            totalIncomeValues.put(DatabaseContract.IncomeEntry.COLUMN_INCOME, String.valueOf(totalIncome));
            totalIncomeValues.put(DatabaseContract.IncomeEntry.COLUMN_SOURCE, "总收入");

            db.update(DatabaseContract.IncomeEntry.TABLE_NAME, totalIncomeValues,
                    DatabaseContract.IncomeEntry._ID + " = ?",
                    new String[] { "1" }); // 假设总收入的行 ID 是 1
        }
        cursor.close();

    }
    public void goBack(View view) {
        finish();
    }


}
